package com.matt.sooncleaner.wifiscan

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.matt.sooncleaner.R
import com.trustlook.wifisdk.wifiscan.WifiItem
import com.trustlook.wifisdk.wifiscan.WifiItemName


class WifiResultViewHolder(itemView: View) : ViewHolder(itemView) {
    val itemNameView: TextView = itemView.findViewById(R.id.item_name)
    val itemValueView: TextView = itemView.findViewById(R.id.item_value)
}

class WifiResultAdapter(val resultItem: MutableList<WifiItem>) :
    RecyclerView.Adapter<WifiResultViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WifiResultViewHolder {
        return WifiResultViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.wifi_result_item, parent, false)
        )
    }

    override fun getItemCount(): Int = resultItem.size

    override fun onBindViewHolder(holder: WifiResultViewHolder, position: Int) {
        val wifiItem = resultItem[position]
        holder.itemNameView.setText(wifiItem.name.getItemName())
        var color = ResourcesCompat.getColor(holder.itemView.resources, R.color.colorSafe, null)
        if (wifiItem.isSafe == 1) {
            color = ResourcesCompat.getColor(holder.itemView.resources, R.color.colorRisk, null)
        }
        holder.itemValueView.setTextColor(color)
        holder.itemValueView.setText(wifiItem.isSafe.parseWifiResult())
    }

    private fun WifiItemName.getItemName(): Int {
        return when(this) {
            WifiItemName.WifiAuth -> R.string.wifi_auth
            WifiItemName.ARPSpoofing -> R.string.arp_spoofing
            WifiItemName.SSLSplit -> R.string.ssl_splitting
            WifiItemName.SSLStrip -> R.string.ssl_stripping
            WifiItemName.DNSSpoofing -> R.string.dns_spoofing
            else -> 0
        }
    }

    private fun Int.parseWifiResult(): Int {
        when (this) {
            0 -> return R.string.safe
            1 -> return R.string.risk
        }
        return 0
    }

}